package guowei.gulidiscount.discount.controller;

import java.util.Arrays;
import java.util.Map;

import guowei.gulidiscount.discount.config.GuliDiscountNacosConfig;
import guowei.gulidiscount.discount.feign.OrderFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import guowei.gulidiscount.discount.entity.CouponEntity;
import guowei.gulidiscount.discount.service.CouponService;
import guowei.common.utils.PageUtils;
import guowei.common.utils.R;

/**
 * 优惠券信息
 *
 * @author guoguo
 * @email 1573746397@qq.com
 * @date 2023-05-21 20:36:02
 */
@RestController
@RequestMapping("/discount/coupon")
@RefreshScope
public class CouponController {
    @Autowired
    private CouponService couponService;

    @Autowired
    private OrderFeignClient orderFeignClient;

    @Autowired
    private GuliDiscountNacosConfig guliDiscountNacosConfig;

    @GetMapping("/nacos/config")
    public R getConfiguration(){
        String msg = guliDiscountNacosConfig.getMsg();
        Integer count = guliDiscountNacosConfig.getCount();
        return R.ok().put("msg",msg).put("count",count);
    }


    @GetMapping("/getOrderInfo/{id}")
    public R getOrder(@PathVariable("id") Integer id){

        CouponEntity couponEntity = new CouponEntity();
        couponEntity.setCouponName("这是测试方法");

//        调用feign接口，实现远程调用
        R one = orderFeignClient.getOneById(id);
        return R.ok().put("discount",couponEntity).put("order",one.get("order"));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("discount:coupon:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = couponService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("discount:coupon:info")
    public R info(@PathVariable("id") Long id){
		CouponEntity coupon = couponService.getById(id);

        return R.ok().put("coupon", coupon);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("discount:coupon:save")
    public R save(@RequestBody CouponEntity coupon){
		couponService.save(coupon);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("discount:coupon:update")
    public R update(@RequestBody CouponEntity coupon){
		couponService.updateById(coupon);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("discount:coupon:delete")
    public R delete(@RequestBody Long[] ids){
		couponService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
